package com.ktwlsoft.fundmanage.service.pictureconfig;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.ktwlsoft.fundmanage.domain.entity.pictureconfig.SignCredential;
import com.ktwlsoft.fundmanage.mapper.pictureconfig.ISignCredentialMapper;
import com.ktwlsoft.fundmanage.payload.BaseResult;
import com.ktwlsoft.fundmanage.payload.IAccountUser;
import com.ktwlsoft.fundmanage.payload.ResultType;
import com.ktwlsoft.fundmanage.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.fundmanage.mapper.pictureconfig.ISignCredentialInfoMapper;
import com.ktwlsoft.fundmanage.domain.entity.pictureconfig.SignCredentialInfo;
import org.springframework.transaction.annotation.Transactional;

/**
 * 要件详情业务层
 *
 * @author zhuhe on 2019-10-08 14:13:23
 * @version 1.0
 */
@Service
public class SignCredentialInfoService{

	/**日志对象*/
	private static Logger logger = LoggerFactory.getLogger(SignCredentialInfoService.class);

	/**要件详情*/
	private final ISignCredentialInfoMapper signCredentialInfoMapper;

	/**要件*/
	private final ISignCredentialMapper signCredentialMapper;

	public SignCredentialInfoService(ISignCredentialInfoMapper signCredentialInfoMapper, ISignCredentialMapper signCredentialMapper) {
		this.signCredentialInfoMapper = signCredentialInfoMapper;
		this.signCredentialMapper = signCredentialMapper;
	}

	/**
 	 * 查询分页 on 2019-10-08 14:13:23
 	 * @param pn 第几页
     * @param pageSize 每页多少条
 	 * @param signCredentialInfo signCredentialInfo
 	 * @return PageInfo<SignCredentialInfo>
 	 */
	public PageInfo<SignCredentialInfo> findPage(int pn,int pageSize,SignCredentialInfo signCredentialInfo) {
		PageHelper.startPage(pn,pageSize);
		PageInfo<SignCredentialInfo> pageInfo = new PageInfo<SignCredentialInfo>(signCredentialInfoMapper.findList(signCredentialInfo));
		return pageInfo;
	}

    /**
	 * 查询集合 on 2019-10-08 14:13:23
	 * @param signCredentialInfo signCredentialInfo
	 * @return List<SignCredentialInfo>
	 */
	public List<SignCredentialInfo> findList(SignCredentialInfo signCredentialInfo) {
		return signCredentialInfoMapper.findList(signCredentialInfo);
	}

    /**
	 * 根据ID查询信息 on 2019-10-08 14:13:23
	 * @param credentialInfoId	主键
	 * @return SignCredentialInfo
	 */
	public SignCredentialInfo getById(String credentialInfoId){
		return signCredentialInfoMapper.getById(credentialInfoId);
	}

   /**
	 * 新增 on 2019-10-08 14:13:23
	 * @param signCredentialInfo signCredentialInfo
	 * @return int
	 */
	public int insert(SignCredentialInfo signCredentialInfo){
		return signCredentialInfoMapper.insert(signCredentialInfo);
	}

   /**
	 * 修改 on 2019-10-08 14:13:23
	 * @param signCredentialInfo signCredentialInfo
	 * @return int
	 */
	public int update(SignCredentialInfo signCredentialInfo){
		return signCredentialInfoMapper.update(signCredentialInfo);
	}

   /**
	 * 根据id集合批量删除信息 on 2019-10-08 14:13:23
	 * @param signCredentialInfoArr	主键编号集合
	 * @return int
	 */
	public int deleteByIds(List<String> signCredentialInfoArr){
		return signCredentialInfoMapper.deleteByIds(signCredentialInfoArr);
	}

   /**
	 * 根据id删除信息 on 2019-10-08 14:13:23
	 * @param credentialInfoId credentialInfoId
	 * @return int
	 */
	public int deleteById(String credentialInfoId){
		return signCredentialInfoMapper.deleteById(credentialInfoId);
	}

	/**
	 * 根据busiId删除信息 on 2019-10-08 14:13:23
	 * @param busiId busiId
	 * @return int
	 */
	public int deleteByBusiId(String busiId){
		return signCredentialInfoMapper.deleteByBusiId(busiId);
	}

	/**
	 *
	 * 控制层调用返回
	 *
	 * */

	/**
	 * 要件详情修改 on 2019-10-08 14:13:23
	 * @param signCredentialInfo signCredentialInfo
	 * @return BaseResult<?>
	 */
	public BaseResult<?> signCredentialInfoUpdate(SignCredentialInfo signCredentialInfo){
		if(signCredentialInfo.getBusiId() == null || "".equals(signCredentialInfo.getBusiId().trim())){
			return BaseResult.body(signCredentialInfo, ResultType.BUSINESS_ERROR).withMessage("业务id为空");
		}
		try {
			signCredentialInfoUpdated(signCredentialInfo);
		}catch (Exception e){
			logger.error(e.getMessage());
			return BaseResult.body(signCredentialInfo, ResultType.BUSINESS_ERROR);
		}
		return BaseResult.ok(signCredentialInfo);
	}

	/**
	 * 要件详情修改 on 2019-10-08 14:13:23
	 * @param signCredentialInfo signCredentialInfo
	 * @return BaseResult<?>
	 */
	@Transactional(rollbackFor = Exception.class)
	public SignCredentialInfo signCredentialInfoUpdated(SignCredentialInfo signCredentialInfo) throws Exception{
		IAccountUser user = UserUtils.getUser();
		signCredentialInfo.setCredentialInfoId(UUID.randomUUID().toString().replace("-",""));
		signCredentialInfo.setCreateTime(new Date());
		signCredentialInfo.setCreateUser(user.getUserId());
		signCredentialInfo.setModifyTime(new Date());
		signCredentialInfo.setModifyUser(user.getUserId());
		signCredentialInfoMapper.deleteByBusiId(signCredentialInfo.getBusiId());
		signCredentialInfoMapper.insert(signCredentialInfo);
		return signCredentialInfo;
	}

	/**
	 * 要件详情删除 on 2019-10-08 14:13:23
	 * @param credentialInfoId credentialInfoId
	 * @return BaseResult<?>
	 */
	public BaseResult<?> signCredentialInfoDelete(String credentialInfoId){
		if("".equals(credentialInfoId.trim())){
			return BaseResult.body(credentialInfoId, ResultType.BUSINESS_ERROR).withMessage("主键id为空");
		}
		SignCredentialInfo signCredentialInfo = signCredentialInfoMapper.getById(credentialInfoId);
		if(signCredentialInfo == null){
			return BaseResult.body(credentialInfoId, ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
		}
		signCredentialInfo.setDeleteMark(1);
		try {
			signCredentialInfoMapper.update(signCredentialInfo);
		}catch (Exception e){
			logger.error(e.getMessage());
			return BaseResult.body(credentialInfoId, ResultType.BUSINESS_ERROR);
		}
		return BaseResult.ok(credentialInfoId);
	}

	/**
	 * 要件详情分页 on 2019-10-08 14:13:23
	 * @param pn 第几页
	 * @param pageSize 每页多少条
	 * @param keyWords keyWords
	 * @return BaseResult<?>
	 */
	public BaseResult<?> signCredentialInfoPage(int pn,int pageSize,String keyWords,String credentialId) {
		SignCredentialInfo signCredentialInfo = new SignCredentialInfo();
		if(keyWords != null && !"".equals(keyWords.trim())){
			signCredentialInfo.setKeyWords(keyWords);
		}
		if(credentialId != null && !"".equals(credentialId.trim())){
			signCredentialInfo.setCredentialId(credentialId);
		}
		PageInfo pageInfo = findPage(pn,pageSize,signCredentialInfo);
		return BaseResult.ok(pageInfo);
	}

	/**
	 * 要件详情详情 on 2019/10/8 14:57
	 *
	 * @param credentialId credentialId
	 * @return BaseResult<?>
	 */
	public BaseResult<?> signCredentialInfoDetails(String credentialId){
		if("".equals(credentialId.trim())){
			return BaseResult.body(credentialId, ResultType.BUSINESS_ERROR).withMessage("主键id为空");
		}
		SignCredentialInfo signCredentialInfo = signCredentialInfoMapper.getById(credentialId);
		if(signCredentialInfo == null){
			return BaseResult.body(credentialId, ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
		}
		return BaseResult.ok(signCredentialInfo);
	}

	/**
	  * 通过busiId查询要件及要件详情数据 on 2019/10/10 9:33
	  *
	  * @param busiId busiId
	  * @param busiCode busiCode
	  * @return BaseResult<?>
	  */
	public BaseResult<?> findByBusiId(String busiId,String busiCode){
		if(busiId == null || "".equals(busiId.trim())){
			return BaseResult.body(busiId,ResultType.BUSINESS_ERROR).withMessage("业务id为空");
		}
		if(busiCode == null || "".equals(busiCode.trim())){
			return BaseResult.body(busiId,ResultType.BUSINESS_ERROR).withMessage("所属业务功能模块代码为空");
		}
		SignCredential signCredential = new SignCredential();
		signCredential.setBusiId(busiId);
		signCredential.setBusiCode(busiCode);
		List<SignCredential> signCredentials = signCredentialMapper.findList(signCredential);
		if(signCredentials != null && signCredentials.size() > 0){
			SignCredentialInfo signCredentialInfo = new SignCredentialInfo();
			signCredentialInfo.setBusiId(busiId);
			List<SignCredentialInfo> infoList = signCredentialInfoMapper.findList(signCredentialInfo);
			if(infoList != null && infoList.size() > 0){
				signCredentials.forEach(value ->{
					infoList.forEach(info ->{
						if(value.getCredentialId().equals(info.getCredentialId())){
							if(value.getInfoList() == null){
								value.setInfoList(Lists.newArrayList());
							}
							value.getInfoList().add(info);
						}
					});
				});
			}
		}
		return BaseResult.ok(signCredentials);
	}

	/**
	  * 新增要件以及要件详情 on 2019/10/10 12:20
	  *
	  * @param signCredentials signCredentials
	  * @param busiId busiId
	  * @return Boolean
	  */
	public Boolean save(List<SignCredential> signCredentials,String busiId){
		if(signCredentials == null || signCredentials.size() <= 0){
			return false;
		}
		if(busiId == null || "".equals(busiId.trim())){
			return false;
		}
		List<SignCredentialInfo> infoList = Lists.newArrayList();
		for(SignCredential signCredential:signCredentials){
			signCredential.setCredentialId(UUID.randomUUID().toString().replace("-",""));
			signCredential.setBusiId(busiId);
			if(signCredential.getInfoList() != null && signCredential.getInfoList().size() > 0){
				for(SignCredentialInfo info:signCredential.getInfoList()){
					info.setCredentialInfoId(UUID.randomUUID().toString().replace("-",""));
					info.setCredentialId(signCredential.getCredentialId());
					info.setBusiId(busiId);
				}
				infoList.addAll(signCredential.getInfoList());
			}
		}
		try {
			signCredentialMapper.inserts(signCredentials);
			signCredentialInfoMapper.inserts(infoList);
		}catch (Exception e){
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * 要件详情删除 on 2019-10-08 14:13:23
	 * @param list list
	 * @return BaseResult<?>
	 */
	@Transactional(rollbackFor = Exception.class)
	public Boolean deleteByBusiIdAndBusiCode(List<SignCredential> list){
		if(list == null || list.size() <= 0){
			return false;
		}
		for(SignCredential value:list){
			if(value.getBusiId() == null || "".equals(value.getBusiId().trim())){
				return false;
			}
			if(value.getBusiCode() == null || "".equals(value.getBusiCode().trim())){
				return false;
			}
		}
		List<SignCredential> credentials = Lists.newArrayList();
		List<String> busiIds = Lists.newArrayList();
		list.forEach(value ->{
			SignCredential signCredential = new SignCredential();
			signCredential.setBusiId(value.getBusiId());
			signCredential.setBusiCode(value.getBusiCode());
			credentials.addAll(signCredentialMapper.findList(signCredential));
			busiIds.add(value.getBusiId());
		});
		if(credentials.size() <= 0){
			return false;
		}
		List<String> credentialIds = Lists.newArrayList();
		credentials.forEach(value ->{
			credentialIds.add(value.getCredentialId());
		});
		try {
			signCredentialMapper.deleteByIds(credentialIds);
			signCredentialInfoMapper.deleteByBusiIds(busiIds);
		}catch (Exception e){
			logger.error(e.getMessage());
			return false;
		}
		return true;
	}
}
